package com.atguigu.gmall.cart.controller;

import com.atguigu.core.bean.Resp;
import com.atguigu.gmall.cart.interceptors.LoginInterceptor;
import com.atguigu.gmall.cart.pojo.Cart;
import com.atguigu.gmall.cart.pojo.UserInfo;
import com.atguigu.gmall.cart.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("cart")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 根据用户id查询登录状态的购物车信息，因为fegin并不能携带token信息（cookie的信息）
     * @return
     */
    @GetMapping("{userId}")
    public Resp<List<Cart>> queryCheckCartsByUserId(@PathVariable("userId") Long userId){
        List<Cart> carts=this.cartService.queryCheckCartsByUserId(userId);
        return Resp.ok(carts);
    }

    @GetMapping("test")
    public String test(HttpServletRequest request){

        UserInfo userInfo = LoginInterceptor.getUserInfo();

        return "hello cart";
    }


    /**
     * 新增购物车
     * @param cart
     * @return
     */
    @PostMapping
    public Resp<Object> addCart(@RequestBody Cart cart) {

        this.cartService.addCart(cart);

        return Resp.ok(null);
    }

    /**
     * 查询购物车
     * @return
     */
    @GetMapping
    public Resp<List<Cart>> queryCarts() {

        List<Cart> carts = this.cartService.queryCarts();

        return Resp.ok(carts);
    }

    /**
     * 修改购物车
     * @param cart
     * @return
     */
    @PostMapping("update")
    public Resp<Object> updateCart(@RequestBody Cart cart) {

        this.cartService.updateCart(cart);

        return Resp.ok(null);
    }

    @PostMapping("delete/{skuId}")
    public Resp<Object> deleteCart(@PathVariable("skuId")Long skuId){
        this.cartService.deleteCart(skuId);
        return Resp.ok(null);
    }
}
